﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure;
using Algorithms;
using System.ComponentModel;

namespace StatisticalTest
{
    public class RootMeanSquareError<RatingType> : PredictionPerformanceMeasurement<RatingType> where RatingType: ICountable, new()
    {
        public override Dictionary<string, double> Score(List<PredictionAlgorithm<RatingType>> lAlgorithmsToCompare, List<User<RatingType>> lUsersList)
        {
            Dictionary<String, double> res = new Dictionary<string,double>();
            Dictionary<String, int> counters = new Dictionary<string,int>();
            //adding all the Algorithms names with the 0 sum
            foreach (PredictionAlgorithm<RatingType> algo in lAlgorithmsToCompare)
            {
                res.Add(algo.Name, 0);
                counters.Add(algo.Name, 0);
            }
            foreach (User<RatingType> user in lUsersList)
            {
                List<Item<RatingType>> hidden = new List<Item<RatingType>>();
                User<RatingType> user2 = user.Split(hidden);
                foreach (PredictionAlgorithm<RatingType> algo in lAlgorithmsToCompare)
                {
                    foreach (Item<RatingType> i in hidden)
                    {
                        res[algo.Name] += Math.Pow(algo.PredictRating(user2, i) - user.Votes[i],2);
                        counters[algo.Name]++;
                    }
                }

            }
            foreach (PredictionAlgorithm<RatingType> algo in lAlgorithmsToCompare)
            {
                res[algo.Name] = Math.Sqrt(res[algo.Name] / counters[algo.Name]);
            }
           return res;
        }
    }
}
